package com.eelpo.cmmp.uac.pages.entitytype;

import java.util.Arrays;
import java.util.List;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.component.label.FeedbackLabel;
import com.eelpo.cmmp.core.wicket.page.extensions.SimpleWebPage;
import com.eelpo.cmmp.uac.dao.vo.EntityType;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IEntityTypeManageService;
import com.eelpo.common.util.BlankUtils;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * <pre>
 * 新增和修改实体类型页面
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class AddOrEditEntityType extends SimpleWebPage {

	@SpringBean
	private IEntityTypeManageService entityTypeManageService;

	/**
	 * 
	 * 新增实体类型构造函数
	 * 
	 */
	public AddOrEditEntityType() {
		this("[新增]实体类型", new EntityType(Status.ENABLED));
	}

	/**
	 * 修改实体类型构造函数
	 * 
	 * @param type
	 *            实体类型
	 */
	public AddOrEditEntityType(final EntityType type) {
		this("[修改]实体类型", type);
	}

	/**
	 * @param title
	 *            页面标题
	 * @param 实体类型
	 *            新增或修改的实体类型
	 */
	public AddOrEditEntityType(String title, final EntityType type) {
		super(title);
		add(renderFormContainer(type));
	}

	public Form<EntityType> renderFormContainer(final EntityType type) {

		Form<EntityType> form = new Form<EntityType>("form", new Model<EntityType>(type));

		renderFeedbackPanel(form);
		renderTypeNameTextField(form);
		renderTypeCodeTextField(form);
		renderTypeStatusDropDownChoice(form);
		renderSaveButton(form);
		renderBackButton(form);

		return form;
	}

	public void renderFeedbackPanel(Form<EntityType> form) {
		form.add(renderFeedbackPanel("feedback"));
	}

	public void renderTypeNameTextField(Form<EntityType> form) {
		TextField<String> txtTypeName = new TextField<String>("txtTypeName", new PropertyModel<String>(form.getDefaultModelObject(), "name"));
		txtTypeName.setMarkupId("txtTypeName");
		form.add(new FeedbackLabel("txtTypeNameFeedback", txtTypeName));
		form.add(txtTypeName);
	}

	public void renderTypeCodeTextField(Form<EntityType> form) {
		TextField<String> txtTypeCode = new TextField<String>("txtTypeCode", new PropertyModel<String>(form.getDefaultModelObject(), "code"));
		txtTypeCode.setMarkupId("txtTypeCode");
		form.add(new FeedbackLabel("txtTypeCodeFeedback", txtTypeCode));
		form.add(txtTypeCode);
	}

	public void renderTypeStatusDropDownChoice(Form<EntityType> form) {
		List<Status> dataList = Arrays.asList(new Status[] { Status.ENABLED, Status.DISABLED });
		IModel<Status> model = new PropertyModel<Status>(form.getDefaultModelObject(), "status");
		IChoiceRenderer<Status> choiceRenderer = new ChoiceRenderer<Status>("label", "value");
		DropDownChoice<Status> actionStatus = new DropDownChoice<Status>("selStatus", model, dataList, choiceRenderer);
		actionStatus.setMarkupId("selStatus");
		form.add(new FeedbackLabel("selStatusFeedback", actionStatus));
		form.add(actionStatus);
	}

	public void renderSaveButton(final Form<EntityType> form) {
		Button btnSave = new Button("btnSave") {

			private static final long serialVersionUID = -1713181203518352588L;

			public void onSubmit() {
				try {
					EntityType type = (EntityType) form.getDefaultModelObject();
					if (BlankUtils.isBlank(type.getId())) {
						type.setId(entityTypeManageService.addEntityType(type));
					} else {
						entityTypeManageService.updateEntityType(type);
					}
					setResponsePage(new EntityTypeManage());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("保存失败！");
				}
			}
		};

		form.add(btnSave);
	}

	public void renderBackButton(final Form<EntityType> form) {
		Link<Void> btnBack = new Link<Void>("btnBack") {

			private static final long serialVersionUID = 4658091880010200920L;

			@Override
			public void onClick() {
				setResponsePage(new EntityTypeManage());
			}
		};

		form.add(btnBack);
	}
}
